import { Router } from "../../utils/common.js";
const db = wx.cloud.database();
Page({
  data: {
    petList: [],
    openid: "",
    userNickName: ""
  },
  onLoad: function (options) {
    let that = this
    if (options && options.openid) {
      that.data.openid = options.openid
    }
  },
  onShow: function () {
    // 获取用户宠物列表
    this.getSomeonePetList();
    // 获取用户信息
    this.getUserInfo();
  },
  /**
   * 获取用户信息
   */
  getUserInfo() {
    db.collection('p_user').where({
      _openid: this.data.openid
    }).get({
      success: res => {
        // 保存用户信息
        this.setData({
          userNickName: res.data[0].nickName
        })
      },
      fail: err => {
        console.log('[数据库] [查询记录] 失败：');
      }
    })
  },
  /**
   * 获取宠物列表
   */
  getSomeonePetList: function () {
    db.collection('p_pets').where({
      _openid: this.data.openid
    }).get({
      success: res => {
        // 保存用户信息
        this.setData({
          petList: res.data
        })
      },
      fail: err => {
        console.log('[数据库] [查询记录] 失败：');
      }
    })
  },
  navToPetDet: function (e) {
    let params = {
      petId: e.currentTarget.dataset.petid
    }
    Router.navigateTo("../dogdet/dogdet", params);
  }
})
